package com.example.hello;

import android.content.Context;
import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.EventChannel;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.HashMap;

class Type {
  static final String GetInfo = "getInfo";
}

/** HelloPlugin */
public class HelloPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  private EventChannel event;

  private EventChannel.EventSink sink;

  private IWXAPI wxapi;

  private String appId = "wxe08cd0876d931ff5";

  // 此处是新的插件加载注册方式
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "hello");
    channel.setMethodCallHandler(this);
    this.createWXAPI(flutterPluginBinding.getApplicationContext());

    event = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "event");
    event.setStreamHandler(new EventChannel.StreamHandler() {
      @Override
      public void onListen(Object o, EventChannel.EventSink eventSink) {
        sink = eventSink;
      }

      @Override
      public void onCancel(Object o) {
      }
    });
  }

  // 创建微信接口
  public void createWXAPI(Context context) {
    wxapi = WXAPIFactory.createWXAPI(context, appId, false);
  }

  // 返回信息
  public void returnInfo() {
    HashMap<String, Object> map = new HashMap<String, Object>();
    HashMap<String, String> result = new HashMap<String, String>();
    result.put("name", "fanzhiliang");
    result.put("age", "24");

    map.put("type", Type.GetInfo);
    map.put("result", result);

    sink.success(map);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method) {
      case "getPlatformVersion":
        result.success("Android " + android.os.Build.VERSION.RELEASE);
        break;
      case "handleAdd":
        HashMap<String, Double> map = (HashMap<String, Double>)call.arguments;
        result.success(map.get("a") + map.get("b"));
        break;
      case "register":
        result.success(wxapi.registerApp(appId));
        break;
      case "openWxApp":
        result.success(wxapi.openWXApp());
        break;
      case "getInfo":
        returnInfo();
        break;
      case "getListTest":
        ArrayList<HashMap> list = new ArrayList<HashMap>();
        HashMap<String, Object> one = new HashMap();
        one.put("name", "fanzhiliang");
        one.put("age", "24");
        list.add(one);

        HashMap<String, Object> two = new HashMap();
        two.put("name", "alcyh");
        two.put("age", "25");
        list.add(two);

        result.success(list);
        break;
      default:
        result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
